package www.albertzhe.manager.http.life;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import www.albertzhe.manager.http.HttpClientOperate;
import www.albertzhe.manager.http.HttpResult;
import www.albertzhe.manager.http.RequestAnnotation;

@Service
public class LinkAnalysis {

    @Resource
    private HttpClientOperate httpClientOperate;

    @RequestAnnotation(path = "/start", name = "登录授权")
    public HttpResult start() {
        FileWriter writer = null;
        try {
            writer = new FileWriter(new File("E:\\xx.txt"), true);
            for (int i = 1; i < 293; i++) {
                String url = "https://www.797ii.com/htm/piclist8/" + i + ".htm";
                HttpResult result = httpClientOperate.doGet(url, null);
                String content = result.getContent();
                content = content.substring(content.indexOf("<div id=\"text\"></div>") + 21, content.indexOf("</ul>"));

                String[] links = content.split("</li><li>");
                for (int j = 0; j < links.length; j++) {
                    String link = links[j];
                    if (j == 0) {
                        link = link.substring(4);
                    }
                    if (j == links.length - 1) {
                        link = link.substring(0, link.length() - 5);
                    }

                    writer.write(link+"\n");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    public HttpResult getText() {
        HttpResult result = httpClientOperate.doGet("https://blog.csdn.net/maoyuanming0806/article/details/77707238", null);
        return result;
    }
}
